<?php
	class Players {

		function __construct(){
			FunctionsHelper::includeFiles(
				array(
					ADMINISTRATOR_MODEL_DIRECTORY . 'model.php',
					ADMINISTRATOR_MODEL_DIRECTORY . 'player.php'
				)
			);
		}

		public function getAll($orderedByName = false){
			$arrPlayers = array();
			$arrResult = $orderedByName == true ? Player::getAllOrderderByName() : Player::getAll();

			foreach($arrResult as $player){
				$newPlayer = new Player($player);
				$newPlayer->setBirthdate(FunctionsHelper::adaptListFormatDate($newPlayer->getBirthdate()));
				$arrPlayers[] = $newPlayer;
				
			}
			return $arrPlayers;
		}
		
		public function getAllOfSex($Sex = 'M', $active = true){
			$arrPlayers = array();
			$arrResult = Player::getAllOfSex($Sex, $active);

			foreach($arrResult as $player){
				$newPlayer = new Player($player);
				$newPlayer->setBirthdate(FunctionsHelper::adaptListFormatDate($newPlayer->getBirthdate()));
				$arrPlayers[] = $newPlayer;
				
			}
			return $arrPlayers;
		}

		public function getPlayer($arrInitialValue = null){
			if (empty($arrInitialValue))
				return new Player();

			$player = new Player($arrInitialValue);

			$player = $player->getPlayer();
			
			$player[0]['birthdate'] = FunctionsHelper::adaptListFormatDate($player[0]['birthdate']);
			if (empty($player))
				return new Player();
			
			return new Player($player[0]);
		}

		public function deletePlayer($playerId){
			$player = $this->getPlayer(
				array(
					'player_id' => $playerId
				)
			);
			return $player->delete();
		}
		
		public function createPlayer($arrInitialValues = null){			
			if (empty($arrInitialValues))
				return array();

			$arrInitialValues['birthdate'] = FunctionsHelper::adaptDBFormatDate($arrInitialValues['birthdate']);
			$player = new Player($arrInitialValues);

			return ($player->create());
		}

		public function updatePlayer($arrPlayer){
			$player = new Player($arrPlayer);
			$player->setBirthdate(FunctionsHelper::adaptDBFormatDate($player->getBirthdate()));
			$player->update();
			return 1;
		}

		public function activatePlayer($id){
			$player = new Player(
				array (
					'player_id' => $id
				)
			);
			
			return $player->activate();
		}
		
		public function getPlayersFromNation($countryId){
			$player = new Player(array('country_id' => $countryId));
			$players = $player->getPlayersWithNation();
			
			if (!empty($players))
				echo json_encode($players);
			else
				echo 0;
		}
		
		public function getPlayersFromNationAndSex($countryId, $sex){
			$player = new Player(array('country_id' => $countryId, 'sex' => $sex));
			$players = $player->getPlayersWithNationAndSex();
			
			if (!empty($players))
				echo json_encode($players);
			else
				echo 0;
		}
	}
?>